<?php

/**
 * @author sunwei  <sunwei@1218.com.cn>
 * Encoding:UTF-8
 * Created Time:2012-12-27 15:41:39 
 */
include_once "globalvar.h";
include_once ("db_local.inc.php");
require_once('/apps/www/htdocs/webservices/include/myXmlParser.class.php');
$dataDB = new DB_Sql($gMysql_host_name, "centerdb", $gMysql_user_name, $gMysql_user_password);

$file_in = file_get_contents("php://input");
$xml = new myXmlParser($file_in);
if(empty($xml->resultArr))
    die('xml is empty!');
foreach ($xml->resultArr[0]['children'] as  $val) {
    foreach ($val['children'] as $v) {
        switch ($v['name']) {
            case 'hostid':
                if (empty($v['data'])) {
                    echo 'error0:hostid is empty!';
                    exit;
                }
                else
                    $barId = $v['data'];
                break;
            case 'sendtime':
                if (empty($v['data'])) {
                    echo 'error1:sendtime is empty';
                    exit;
                }
                else
                    $sendTime = $v['data'];
                break;
            case 'wan':
                $wanInfo[] = $v['attrs'];
                break;
            case 'adslclient':
                $adslInfo[] = $v['attrs'];
                break;
            case 'macbind':
                $macbindInfo[] = $v['attrs'];
                break;
        }
    }
}
$w = 0;
$a = 0;
$i = 0;
$sql = "delete from barlinkinfo where barId='" . $barId . "'";
$dataDB->query($sql);

if (!empty($wanInfo)) {
    foreach ($wanInfo as $wan) {
        $w++;
        $insert = "insert into barlinkinfo (`barId`,`sendTime`,`dataType`,`ifname`,`mac`,`access`,`ip`,`mask`,`gateway`,`flow`,`route2level`) values" .
                "('" . $barId . "','" . $sendTime . "',0,'" . $wan['ifname'] . "','" . $wan['mac'] . "','" . $wan['access'] . "','" . $wan['ip'] . "','" . $wan['mask'] . "','" . $wan['gateway'] . "','" . $wan['flow'] . "','" . $wan['route2level'] . "')";
        $dataDB->query($insert);
    }
}
if (!empty($adslInfo)) {
    foreach ($adslInfo as $adsl) {
        $a++;
        $insert = "insert into barlinkinfo (`barId`,`sendTime`,`dataType`,`ifname`,`name`,`username`,`password`,`in_flow`,`out_flow`) values" .
                "('" . $barId . "','" . $sendTime . "',1,'" . $adsl['ifname'] . "','" . $adsl['name'] . "','" . $adsl['username'] . "','" . $adsl['password'] . "','" . $adsl['in_flow'] . "','" . $adsl['out_flow'] . "')";
        $dataDB->query($insert);
    }
}
if (!empty($macbindInfo)) {
    //删除一些历史记录
    $sql = "delete from barlinkipinfo where barId='" . $barId . "' and sendTime<'" . date('YmdHis', strtotime("-2 month")) . "'";
    $dataDB->query($sql);
    foreach ($macbindInfo as $macbind) {
        $i++;
        $insert = "insert into barlinkipinfo  values ('','" . $barId . "','" . $sendTime . "','" . $macbind['ip'] . "','" . $macbind['mac'] . "','" . $macbind['in_flow'] . "','" . $macbind['out_flow'] . "','" . $macbind['lastactivetime'] . "')";
        $dataDB->query($insert);
    }
}
echo 'ok!', $barId, ' at ', $sendTime, 'send  wan:', $w, 'adslclient:', $a, 'ip:', $i;
?>

